package com.bdy.translate.util

import android.graphics.Color
import android.util.Log
import android.view.Gravity
import android.widget.Toast
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.fenghuajueli.libbasecoreui.baseswitch.SwitchKeyUtils
import com.fenghuajueli.libbasecoreui.constants.AppConfigInfo
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils

object TranslateUtil {
    val keyName by lazy { "${AppConfigInfo.APPLICATION_ID}_translate" }
    const val defaultSecretKey =
        "115791935"

    fun isReachingLimit(): Boolean {
        val wordCount = MmkvUtils.get(TRANSLATION_WORD_COUNT, 0)
        Log.e("TAG", wordCount.toString())
        val limit = if (SwitchKeyUtils.getPayStatus()) 10000 else 3000
        return if (wordCount > limit) {
            val transDate = MmkvUtils.get(TRANSLATION_DATE, 0L)
            if (DateUtil.calculateDayGap(transDate, System.currentTimeMillis()) >= 1) {
                MmkvUtils.save(TRANSLATION_WORD_COUNT, 0)
                false
            } else {
                true
            }
        } else {
            false
        }
    }

    fun saveWordCount(count: Int){
        val wordCount = MmkvUtils.get(TRANSLATION_WORD_COUNT, 0)
        LogUtils.d(wordCount)
        MmkvUtils.save(TRANSLATION_WORD_COUNT, wordCount + count)
        MmkvUtils.save(TRANSLATION_DATE, DateUtil.getDayStart())
    }

    fun makeToast(msg: String, duration: Int = Toast.LENGTH_SHORT) {
        ToastUtils.make().setGravity(Gravity.BOTTOM, 0, 200)
            .setBgColor(Color.parseColor("#000000"))
            .setTextColor(Color.parseColor("#ffffff"))
            .show(msg, duration)
    }

    private const val TRANSLATION_WORD_COUNT = "TRANSLATION_WORD_COUNT"
    private const val TRANSLATION_DATE = "TRANSLATION_DATE"
}